package org.android.brasil.projetos.oktoberfest2011.view.controller;

import java.util.ArrayList;
import java.util.List;

import org.android.brasil.projetos.oktoberfest2011.R;
import org.android.brasil.projetos.oktoberfest2011.negocio.model.Tweet;
import org.android.brasil.projetos.oktoberfest2011.negocio.servico.TwitterHelper;
import org.android.brasil.projetos.oktoberfest2011.utils.AnalyticsUtils;
import org.android.brasil.projetos.oktoberfest2011.utils.Constantes;
import org.android.brasil.projetos.oktoberfest2011.utils.ControleDeBackground;
import org.android.brasil.projetos.oktoberfest2011.view.PullToRefreshListView;
import org.android.brasil.projetos.oktoberfest2011.view.PullToRefreshListView.OnRefreshListener;
import org.android.brasil.projetos.oktoberfest2011.view.TwitterItemAdapter;

import android.app.Activity;
import android.app.Dialog;
import android.app.ProgressDialog;
import android.content.Context;
import android.net.ConnectivityManager;
import android.os.AsyncTask;
import android.os.Bundle;
import android.os.Handler;
import android.util.Log;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.Toast;

public class TwitterController extends Activity {

	List<Tweet> tweets = new ArrayList<Tweet>();
	Handler handler = new Handler();
	private AnalyticsUtils mGa;
	private ProgressDialog dialog;
	private OnClickListener casaListener = new OnClickListener() {

		public void onClick(View v) {
			setResult(RESULT_OK);
			finish();

		}
	};

	
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		
		this.mGa = AnalyticsUtils.getInstance(this);
		
		setContentView(R.layout.twitter_view);
		ImageView casa = (ImageView) findViewById(R.id.casa);
		casa.setOnClickListener(casaListener);
		RelativeLayout rl = (RelativeLayout) findViewById(R.idTwitter.relativeLayout01);
		ControleDeBackground.SetBarraSuperiorBackGround(getApplicationContext(),rl);
		this.showDialog(0);
		this.loadData();
		PullToRefreshListView listView = (PullToRefreshListView) findViewById(R.idTwitter.listView);
		listView.setOnRefreshListener(new OnRefreshListener() {
		    public void onRefresh() {
		        new GetDataTask().execute();
		    }
		});
		
		this.mGa.trackScreen(getString(R.string.oktoberfestTwitter));
	}

	private void loadData() {
		if (this.isOnline()) {
			try {
				new Thread() {
					public void run() {
						tweets = TwitterHelper.retrieveTweets(Constantes.TWEETER_QUERY, Constantes.MAX_TWEETS_COUNT, 1);
						handler.post(new Runnable() {
	                    	public void run() {
	                    		finishLoading();
	                    	}
	                	});
					}
				}.start();
			} catch (RuntimeException e) {
				Log.e(this.getClass().getSimpleName(), e.getMessage(), e);
				Toast.makeText(TwitterController.this, TwitterController.this.getString(R.string.err_carregamento_twitter), Toast.LENGTH_LONG).show();
			}
		} else {
			Toast.makeText(TwitterController.this, TwitterController.this.getString(R.string.err_offline), Toast.LENGTH_LONG).show();
			this.finish();
		}
	}

	protected void finishLoading() {
		PullToRefreshListView listView = (PullToRefreshListView) findViewById(R.idTwitter.listView);
		listView.setAdapter(new TwitterItemAdapter(TwitterController.this, R.layout.twitter_item_view, tweets));
		if(dialog != null && dialog.isShowing()){
			TwitterController.this.dismissDialog(0);
		}
	}
	
	@Override
	protected Dialog onCreateDialog(int id) {
		dialog = new ProgressDialog(this);
        dialog.setTitle(this.getString(R.string.oktoberfestTwitter));
        dialog.setMessage(this.getString(R.string.carregando));
        dialog.setIndeterminate(true);
        dialog.setCancelable(true);
        return dialog;
	}

	public boolean isOnline() {
		ConnectivityManager cm = (ConnectivityManager) getSystemService(Context.CONNECTIVITY_SERVICE);
		return cm.getActiveNetworkInfo()!=null && cm.getActiveNetworkInfo().isConnectedOrConnecting();
	}
	
	private class GetDataTask extends AsyncTask<Void, Void, String[]> {
	    @Override
	    protected void onPostExecute(String[] result) {
	    	PullToRefreshListView listView = (PullToRefreshListView) findViewById(R.idTwitter.listView);
	    	listView.onRefreshComplete();
	        super.onPostExecute(result);
	    }

		@Override
		protected String[] doInBackground(Void... params) {
			tweets = TwitterHelper.retrieveTweets(Constantes.TWEETER_QUERY, Constantes.MAX_TWEETS_COUNT, 1);
			return null;
		}
	}
	
	protected void onDestroy() {
		super.onDestroy();
	}
}

